package com.zcs.shardingjdbc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcs.shardingjdbc.entity.Order;
import com.zcs.shardingjdbc.service.OrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

/**
 * @author Houger.
 * @date 2024年08月16日 11:18.
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @GetMapping("/add/{date}")
    public String add(@PathVariable("date")String date){

        System.out.println("date="+date);
        final Order order = new Order();
        order.setId(date+ UUID.randomUUID().toString().replace("_","").replace("-",""));
        order.setCustomerId(1);
        order.setTotalAmount(BigDecimal.ZERO);
        final boolean save = orderService.save(order);
        return order.getId();
    }

    @GetMapping("/query/{id}")
    public String query(@PathVariable("id")String id){
        final Order order = orderService.getById(id);
        return Optional.ofNullable(order).map(Objects::toString).orElseThrow(()->new RuntimeException("Not found from db"));
    }
}
